//##############################################################################
//#    CuteWebshot is an application which can be used to render
//#    screenshots from webpages.
//#
//#    Copyright (C) 2010  Juhapekka Piiroinen
//#
//#    This program is free software: you can redistribute it and/or modify
//#    it under the terms of the GNU General Public License as published by
//#    the Free Software Foundation, either version 3 of the License, or
//#    (at your option) any later version.
//#
//#    This program is distributed in the hope that it will be useful,
//#    but WITHOUT ANY WARRANTY; without even the implied warranty of
//#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//#    GNU General Public License for more details.
//#
//#    You should have received a copy of the GNU General Public License
//#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//#############################################################################
//# Contact: juhapekka.piiroinen@gmail.com
//#############################################################################
#include "filetostringlistthread.h"

void FileToStringListThread::run()
{
    FUNC_IN;
        QStringList list;
        QFile fileRead(m_filename);
        m_bStop = false;
        if (fileRead.open(QIODevice::ReadOnly)) {
            DEBUG << "file opened read only";
            int i = 0;
            while (fileRead.atEnd()==false && m_bStop==false) {
                i++;
                QString line = QString(fileRead.readLine());
                QStringList lineData = line.split(",");
                if (lineData.count()==2) {
                    QString url = lineData.at(1).trimmed();
                    QUrl tmpUrl = QUrl::fromUserInput(url);

                    // lets check the validity of the url
                    if (tmpUrl.isValid() && tmpUrl.isRelative()==false) {
                        if (url.startsWith("http")==false) {
                            url = "http://" + url;
                        }

                        // lets update status on each 10k urls
                        if (i%10000==0) {
                            emit status(QString("Imported %0 urls").arg(QString::number(i)));
                        }

                        list << url;
                    }
                }
            }
            fileRead.close();
            if (m_model) {
                m_model->setStringList(list);
            }
        }
        DEBUG << "Found" << list.count() << "urls.";
        emit ready(list);
    FUNC_OUT;
}
